package com.sina.shop.util;

import org.apache.shiro.crypto.hash.SimpleHash;

/**
 * Created with IntelliJ IDEA.
 * Description:  MD5摘要 生成类
 * 段浩杰   2017-11-03-13:24
 */
public class MD5Utils {

    /**
     * 加密算法
     *
     * @param encryptName  算法名字
     * @param encryptStr   加密的字符串
     * @param salt         盐
     * @param encryptTimes 加密次数
     * @param isHex        true是用hex加密存储/false用Base64加密存储
     * @return
     */
    public static String getEncryptString(String encryptName, String encryptStr, String salt, Integer encryptTimes, boolean isHex) {
        if (!StringUtils.isEmpty(encryptStr)) {
            if (isHex) {
                return new SimpleHash(encryptName, encryptStr, salt, encryptTimes).toHex();
            } else {
                return new SimpleHash(encryptName, encryptStr, salt, encryptTimes).toBase64();
            }
        }
        return null;
    }

    //测试
    public static void main(String[] args) {
        //4280d89a5a03f812751f504cc10ee8a5
        System.out.println(getEncryptString("MD5", "123456", null, 2, true));
    }
}
